package com.koi.system.service.user;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koi.common.constants.KoiConstants;
import com.koi.common.ex.koi.YUtil;
import com.koi.common.satoken.KoiLoginUtil;
import com.koi.model.vo.system.SysLoginUserVo;
import com.koi.plus.mapper.system.SysLoginUserMapper;
import com.koi.pojo.system.SysDept;
import com.koi.pojo.system.SysLoginUser;
import com.koi.pojo.system.SysRole;
import com.koi.system.service.dept.SysDeptService;
import com.koi.system.service.role.SysRoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ･ᴗ･
 * @description [用户信息表-服务实现层]
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysLoginUserServiceImpl extends ServiceImpl<SysLoginUserMapper, SysLoginUser> implements SysLoginUserService {

    private final StpInterface stpInterface;

    private final SysLoginUserMapper sysLoginUserMapper;

    private final SysDeptService sysDeptService;

    private final SysRoleService sysRoleService;

    /**
     * @description [用户表、部门表左连接查询]
     * @author ･ᴗ･
     */
    @Override
    public IPage<SysLoginUser> listPage(SysLoginUserVo vo) {
        // 分页构造器
        Page<SysLoginUser> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        // 进行分页查询
        page = sysLoginUserMapper.listPage(page, vo);
        return page;
    }

    /**
     * @description [获取用户信息、角色、按钮等权限、左侧菜单栏数据(另外一个接口)]
     * @author ･ᴗ･
     */
    @Override
    public Map<String, Object> getLoginUserInformation() {
        YUtil.isTrue(ObjectUtils.isEmpty(StpUtil.getLoginIdAsLong()), "登录认证失效，请重新登录");
        LambdaQueryWrapper<SysLoginUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(SysLoginUser::getUserId, SysLoginUser::getUserName, SysLoginUser::getAvatar);
        wrapper.eq(SysLoginUser::getUserId, StpUtil.getLoginIdAsLong());
        SysLoginUser sysLoginUser = sysLoginUserMapper.selectOne(wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("loginUser", sysLoginUser);
        map.put("roles", stpInterface.getRoleList(StpUtil.getLoginId(), StpUtil.getLoginType()));
        // 用户拥有的权限按钮
        map.put("buttons", stpInterface.getPermissionList(StpUtil.getLoginIdAsLong(), StpUtil.getLoginType()));
        return map;
    }

    /**
     * @description [个人中心-左侧卡片数据]
     * @author ･ᴗ･
     */
    @Override
    public Map<String, Object> getPersonalData() {
        SysLoginUser sysLoginUser = this.getById(KoiLoginUtil.getUserId());
        Map<String, Object> map = new HashMap<>();
        map.put("loginName", sysLoginUser.getLoginName());
        map.put("userName", sysLoginUser.getUserName());
        map.put("avatar", sysLoginUser.getAvatar());
        map.put("phone", sysLoginUser.getPhone());
        map.put("email", sysLoginUser.getEmail());
        map.put("sex", sysLoginUser.getSex());
        map.put("createTime", sysLoginUser.getCreateTime());
        String deptId = sysLoginUser.getDeptId();
        if (StringUtils.isBlank(deptId)) {
            map.put("deptName", "无名部门");
        }
        LambdaQueryWrapper<SysDept> deptWrapper = new LambdaQueryWrapper<>();
        deptWrapper.eq(SysDept::getDeptStatus, KoiConstants.ZERO_STRING);
        deptWrapper.eq(SysDept::getDeptId, deptId);
        SysDept sysDept = sysDeptService.getOne(deptWrapper);
        if (ObjectUtils.isEmpty(sysDept) || StringUtils.isBlank(sysDept.getDeptName())) {
            map.put("deptName", "无名部门");
        } else {
            map.put("deptName", sysDept.getDeptName());
        }
        List<Long> roleIdList = sysRoleService.listRightRole(KoiLoginUtil.getUserId());
        if (CollectionUtil.isEmpty(roleIdList)) {
            map.put("roleName", "无名角色");
        } else {
            LambdaQueryWrapper<SysRole> roleWrapper = new LambdaQueryWrapper<>();
            roleWrapper.select(SysRole::getRoleName);
            roleWrapper.eq(SysRole::getRoleStatus, KoiConstants.ZERO_STRING);
            roleWrapper.in(SysRole::getRoleId, roleIdList);
            roleWrapper.orderByAsc(SysRole::getRoleId);
            List<SysRole> roleList = sysRoleService.list(roleWrapper);
            List<String> roleCollectList = roleList.stream().map(SysRole::getRoleName).collect(Collectors.toList());
            String roleName = String.join("/", roleCollectList);
            map.put("roleName", roleName);
        }
        return map;
    }

    /**
     * @description [多条件数据导出]
     * @author ･ᴗ･
     */
    @Override
    public List<SysLoginUser> listLoginUser(SysLoginUserVo vo) {
        return sysLoginUserMapper.listLoginUser(vo);
    }

}
